#include "MGAcore.hpp"


MGAcore::MGAcore(Validator *val,GAmodSettings &settings) throw(MGAcoreException)
: SimpleCore(val,settings)
{
};




int MGAcore::runGeneration(int num)
{
int i,e;

this->myValidator->valuta_pop(M);
//this->numCrossover = 0;
this->numMut = 0;
	
for(i=0;i<num;i++)
{
	
	M->selection_tournament_elitism((*N),this->t_size,this->pt);
	int s=N->get_size();
	int r=0;
	Individuo ** individuals = (Individuo**)malloc(s*sizeof(Individuo*));
	if(individuals==NULL)
		{
			printf("[ERROR] Memory Allocation failed\n");
			exit(1);
		}
	for (e=0;e<(s/2);e++)
		{
			Individuo * mother = N->get(e);
			r=(int)(NORM_RAND*MAX_TS*2)%s;
			Individuo * father =N->get(r);
			Individuo * s1= new Individuo(DEF_ROWS,DEF_COLS);
			Individuo * s2= new Individuo(DEF_ROWS,DEF_COLS);
			mother->getOffspring(father,s1,s2);
			numMut+=s1->mutate(5);
			numMut+=s2->mutate(5);
			individuals[2*e]=s1;
			individuals[2*e+1]=s2;
		}
	delete(M);
	N= new Popolazione(s);
	M= new Popolazione(s);
	M->load_list(s,individuals);
	this->myValidator->valuta_pop(M);
}	
return i;		
}
